package com.lucun.es_transaction.service.impl;

import com.lucun.es_transaction.entity.Classification;
import com.lucun.es_transaction.entity.Commodity;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.ClassificationMapper;
import com.lucun.es_transaction.mapper.CommodityMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.CommodityService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    UserLogMapper userLogMapper;
    @Autowired
    ClassificationMapper classificationMapper;
    Commodity commodity=new Commodity();
    @Override
    public JsonResult<?> insert(Commodity commodity, HttpServletRequest request) {//商品添加
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据不能为空");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        int num = 0;
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            num = random.nextInt(9);
            stringBuffer.append(num);
        }
        commodity.setComNumber(stringBuffer.toString());
        if (ObjectUtils.isEmpty(commodity.getComNumber())){
            throw  new BaseException("商品编号不能为空");
        }
        if (ObjectUtils.isEmpty(commodity.getClassificationId())){
            throw  new BaseException("分类编号不能为空");
        }
        if (ObjectUtils.isEmpty(commodity.getComName())||commodity.getComName().length()>11||commodity.getComName().length()<2){
            throw  new BaseException("商品名称不能为空且长度要位于2-11位之间");
        }
        if (ObjectUtils.isEmpty(commodity.getComPrice())){
            throw  new BaseException("商品价格不能为空");
        }
        if (ObjectUtils.isEmpty(commodity.getOldPrice())){
            throw  new BaseException("商品原价格不能为空");
        }

        commodity.setComSale(0);
        if (ObjectUtils.isEmpty(commodity.getComTotal())){
            throw  new BaseException("商品数量不能为空");
        }
        if (commodity.getComTotal()==0){
            throw  new BaseException("商品数量不能为0");
        }
        commodity.setCreateTime(new Date());//创建时间
        commodity.setStatus((byte)1);
        commodity.setUserId(idByToken);
        int i = commodityMapper.insertSelective(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"添加"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"添加"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","添加成功",commodity);
    }
    @Override
    public JsonResult<?> updateByPrimaryKey(Commodity commodity,HttpServletRequest request) {//商品下架
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        int i=commodityMapper.updateByPrimaryKey(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"下架"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"下架"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        }

        return new JsonResult("200","下架成功");
    }
    @Override
    public JsonResult<?> updateByPrimaryKeySelective(Commodity commodity, HttpServletRequest request) {//商品修改
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        commodity.setUpdateTime(new Date());
        commodity.setStatus((byte)1);
        int i=commodityMapper.updateByPrimaryKeySelective(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+commodity.getComName()+"商品信息"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+commodity.getComName()+"商品信息"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","修改成功",commodity);
    }
    @Override
    public JsonResult<?> selectByPrimaryKey(Commodity commodity, HttpServletRequest request) {//商品查询id
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        Commodity i=commodityMapper.selectByPrimaryKey(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(commodity)){
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+i.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+i.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }

    @Override
    public JsonResult<?> selectBynumber(Commodity commodity, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        Commodity i=commodityMapper.selectBynumber(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(commodity)){
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"使用商品编号查询"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"使用商品编号查询"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",commodity);
    }

    @Override
    public JsonResult<?> selectnumber(int comNumber, HttpServletRequest request) {
        return null;
    }

    @Override
    public JsonResult<?> selectByname(Commodity commodity, HttpServletRequest request) {//商品查询/模糊
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        List<Commodity> i=commodityMapper.selectByname(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i==null){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }

    @Override
    //商品关键字查询按评论量排列
    public JsonResult<?> selectBynameeva(Commodity commodity, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        List<Commodity> i=commodityMapper.selectBynameeva(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i==null){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }
    @Override
    public JsonResult<?> selectBynameASC(Commodity commodity, HttpServletRequest request) {//商品关键字查询价格升序排列
        List<Commodity> i=commodityMapper.selectBynameASC(commodity);
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i==null){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }
    @Override
    public JsonResult<?> selectBynameDESC(Commodity commodity, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        List<Commodity> i=commodityMapper.selectBynameDESC(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i==null){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getComName()+"商品"+"失败");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }
    @Override
    public JsonResult<?> selectBycategory(Commodity commodity, HttpServletRequest request) {//商品查询/类别
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        Commodity i=commodityMapper.selectBycategory(commodity);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(commodity)){
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getClassificationId()+"类商品"+"失败");
            userLogMapper.insert(log);

        } else {
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"查询"+commodity.getClassificationId()+"类商品"+"成功");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","查询成功",i);
    }
    @Override
    public List<Commodity> getProductList(String sqlId) {
        return commodityMapper.getProductList(sqlId);
    }
    @Override
    public JsonResult<?> getuserId(Commodity commodity,HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        commodity.setUserId(idByToken);
        List<Commodity> i=commodityMapper.getuserId();
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i==null){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"查询个人发布商品"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"查询个人发布商品"+"失败");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(commodity)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }
}
